﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using skags.Models;

namespace skags.Controllers
{
/**
* University of Reykjavík
* Brjóstagjöf.is is the product of first-year practical programming course
* for computer science students at said university
*
* URL: http://code.google.com/p/scags
* Authors:
* Ástvaldur Sigurðsson
* Guðjón Jónsson
* Kristín Inga Vigfúsdóttir
* Steinar Bragi Sigurðsson
* Sveinn Ernstsson
*/
    [HandleError]
    [LogError]
    public class HomeController : Controller
    {
        /**
        * Stjórnar /Home/*
        */
        DBDataContext _database = new DBDataContext();
        DBRepository dbRep = new DBRepository();
        PageRepository pageRep = new PageRepository();
        NewsRepository newsRep = new NewsRepository();
        ArticleRepository artRep = new ArticleRepository();
        PollRepository pollRep = new PollRepository();

        public ActionResult Index()
        {
            ViewData["Pages"] = pageRep.GetAllPagesByCategory(1); // 1 = Forsíða
            ViewData["NewestNews"] = newsRep.GetNewestByAmount(5); // Skilar 5 nýjustu fréttum
            ViewData["NewestArticles"] = artRep.GetNewestByAmount(5); // Skilar 5 nýjustu greinum
            ViewData["TopNews"] = newsRep.GetMostReadByAmount(5); // Skilar 5 mest lesnu fréttum
            ViewData["TopArticles"] = artRep.GetMostReadByAmount(5); // Skilar 5 mest lesnu greinum
            var poll = pollRep.GetNewestPoll();

            dbRep.UpdateViewCount("Home", "Index");

            if (poll != null)
            {
                return View(poll);
            }

            else
            {
                return View();
            }

            
        }

        public ActionResult About()
        {
            ViewData["Pages"] = pageRep.GetAllPagesByCategory(2); // 2 = Um okkur

            return View();
        }

        public ActionResult Page(int? id)
        {
            if(!id.HasValue)
                throw new Exception( "Engin síða valin!" );

            int pageid = id.Value;

            var thePage = (from s in _database.Pages
                           where s.ID == pageid
                           select new SubPageViewModel
                                      {
                                          ID = s.ID,
                                          Title = s.Title,
                                          DateCreated = s.DateCreated,
                                          Text = s.Text,
                                          ParentID = ( s.SubPage == false ? s.ParentID : _database.Pages.Where(p=>p.ID == s.ParentID).SingleOrDefault().ParentID )
                                      }).SingleOrDefault();

            var theNav = pageRep.GetAllPagesByCategory(thePage.ParentID);

            if( thePage == null )
                throw new Exception( "Þessi síða er ekki til!" );

            return View(new DisplayPageViewModel { Page = thePage, Nav = theNav });
        }

        public ActionResult Help()
        {
            return View();
        }

    }
}
